package com.llg.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llg.common.CustomException;
import com.llg.common.R;
import com.llg.entity.Category;
import com.llg.entity.Dish;
import com.llg.entity.Setmeal;
import com.llg.mapper.CategoryMapper;
import com.llg.service.CategoryService;
import com.llg.service.DishService;
import com.llg.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类
     * 如果有对应菜品或者套餐绑定该分类则不能删
     *
     * @param id
     * @return
     */
    @Override
    public void remove(Long id) {
        //查询是否关联菜品数据
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        if (count1 > 0) {
            //说明绑定了菜品
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }


        //查询是否关联套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);

        if (count2 > 0) {
            //说明绑定了套餐
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        super.removeById(id);
    }
}
